package com.sinosoft.system.primary.dao.imp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.sinosoft.system.primary.dao.SysButtonRelDao;
import com.sinosoft.system.primary.entity.SysButtonRel;
import com.sinosoft.system.primary.entity.SysRole;


   /**
    * sys_button_rel DaoJdbc实现类
    * Fri Dec 23 15:06:05 CST 2016 shangxp
    */ 
@Repository
public class SysButtonRelDaoJdbc implements SysButtonRelDao {
	private static Logger log = Logger.getLogger(SysButtonRelDaoJdbc.class);
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public int insertSysButtonRel(SysButtonRel sysButtonRel) {
		String sqlStr = "insert into sys_button_rel ( id, role_code, menu_code, button_code, is_del) values(  ?, ?, ?, ?, 0 )";
		log.debug(sqlStr+"\n"+sysButtonRel.toString());
		try{
			Object[] args = {
				sysButtonRel.getId(),
				sysButtonRel.getRoleCode(),
				sysButtonRel.getMenuCode(),
				sysButtonRel.getButtonCode()
			};
			return jdbcTemplate.update(sqlStr, args);
		}catch(Exception e){
			log.error("数据添加异常 SQL："+sqlStr+"\n"+sysButtonRel.toString()+e.getMessage());
		}
		return 0;
	}

	@Override
	public int updateSysButtonRel(SysButtonRel sysButtonRel) {
		String sqlStr = "update sys_button_rel set id = ? , role_code = ? , menu_code = ? , button_code = ?  where id = ?";
		log.debug(sqlStr+"\n"+sysButtonRel.toString());
		try{
			Object[] args = {
					sysButtonRel.getId(),
					sysButtonRel.getRoleCode(),
					sysButtonRel.getMenuCode(),
					sysButtonRel.getButtonCode(),
					sysButtonRel.getId()
			};
			return jdbcTemplate.update(sqlStr, args);
		}catch(Exception e){
			log.error("数据更新异常 SQL："+sqlStr+"\n"+sysButtonRel.toString()+e.getMessage());
		}
		return 0;
	}

	@Override
	public int deleteSysButtonRel(int id) {
		String sqlStr = "update sys_button_rel set is_del = 1  where id = ?";
		try{
			Object[] args = {
					id
			};
			return jdbcTemplate.update(sqlStr, args);
		}catch(Exception e){
			log.error("数据删除异常"+sqlStr+"\nID="+id+"\n"+e.getMessage());
		}	return 0;
	}

	@Override
	public SysButtonRel selectSysButtonRel(int id) {
		String sqlStr = "select * from sys_button_rel where is_del =0 and id = ?";
		log.debug(sqlStr+"\nID="+id);
		try{
			Object[] args = {
				id
			};
			return jdbcTemplate.queryForObject(sqlStr, args, new SysButtonRelMapper());
		}catch(Exception e){
			log.error("数据删除异常"+sqlStr+"\nID="+id+"\n"+e.getMessage());
		}	return null;
	}

	@Override
	public SysButtonRel selectSysButtonRel(String[] queryKeys,Object[] queryValues) {
		StringBuffer sqlStr = new StringBuffer("select * from sys_button_rel where is_del =0 " );
		if(queryKeys!=null && queryKeys.length!=0){
			for(int i=0;i<queryKeys.length;i++){
				sqlStr.append(" and "+queryKeys[i]+"=? ");
			}
		}
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.queryForObject(sqlStr.toString(), queryValues, new SysButtonRelMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public int selectCountSysButtonRel() {
		String sqlStr = "select count(1) from sys_button_rel where is_del =0 ";
		log.debug(sqlStr);
		try{
			return jdbcTemplate.queryForObject(sqlStr, Integer.class);
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\n"+e.getMessage());
		}
		return 0;
	}

	@Override
	public List<SysButtonRel> selectLimitSysButtonRel(int start, int end) {
		String sqlStr = "select * from sys_button_rel where is_del =0  limit ?,?";
		log.debug(sqlStr);
		try{
		Object[] args = {
				start,
				end
		};
			return jdbcTemplate.query(sqlStr, args, new SysButtonRelMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public List<SysButtonRel> selectAllSysButtonRel() {
		String sqlStr = "select * from sys_button_rel where is_del =0 ";
		log.debug(sqlStr);
		try{
			return jdbcTemplate.query(sqlStr, new SysButtonRelMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public int selectCountSysButtonRel(String[] queryKeys,Object[] queryValues) {
		StringBuffer sqlSb = new StringBuffer("select count(1) from sys_button_rel where is_del = 0 ");
		if(queryKeys!=null && queryKeys.length!=0){
			for(int i=0;i<queryKeys.length;i++){
				sqlSb.append(" and "+queryKeys[i]+"=? ");
			}
		}		
		log.debug(sqlSb.toString());
		try{
			return jdbcTemplate.queryForObject(sqlSb.toString(), queryValues,Integer.class);
		}catch(Exception e){
			log.error("数据查询异常"+sqlSb.toString()+"\n"+e.getMessage());
		}
		return 0;
	}

	@Override
	public List<SysButtonRel> selectLimitSysButtonRel(String[] queryKeys,Object[] queryValues) {
		StringBuffer sqlStr = new StringBuffer("select * from sys_button_rel where is_del =0 " );
		if(queryKeys!=null && queryKeys.length!=0){
			for(int i=0;i<queryKeys.length-2;i++){
				sqlStr.append(" and "+queryKeys[i]+"=? ");
			}
		}
		sqlStr.append(" limit ?,?");
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.query(sqlStr.toString(), queryValues, new SysButtonRelMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public List<SysButtonRel> selectAllSysButtonRel(String[] queryKeys,Object[] queryValues) {
		StringBuffer sqlStr = new StringBuffer("select * from sys_button_rel where is_del =0 " );
		if(queryKeys!=null && queryKeys.length!=0){
			for(int i=0;i<queryKeys.length;i++){
				sqlStr.append(" and "+queryKeys[i]+"=? ");
			}
		}
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.query(sqlStr.toString(), queryValues, new SysButtonRelMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return null;
	}

	protected class SysButtonRelMapper implements RowMapper<SysButtonRel> {
		@Override
		public SysButtonRel mapRow(ResultSet arg0, int arg1) throws SQLException {
			SysButtonRel sysButtonRel = new SysButtonRel();
			if(arg0!=null){
				sysButtonRel.setId(arg0.getInt("id"));
				sysButtonRel.setRoleCode(arg0.getString("role_code"));
				sysButtonRel.setMenuCode(arg0.getString("menu_code"));
				sysButtonRel.setButtonCode(arg0.getString("button_code"));
				}
			return sysButtonRel;
		}  
	}

	@Override
	public List<SysButtonRel> selectRoleSysButtonRels(List<SysRole> sysRoles) {
		String sqlStr = "select distinct m.* from sys_button_rel where is_del=0 and r.id in (";
		try{
			if(sysRoles!=null && !sysRoles.isEmpty()){
				Object[] args = new Object[sysRoles.size()];
				for(int i=0;i<sysRoles.size();i++){
					SysRole sysRole = sysRoles.get(i);
					sqlStr += "?, ";
					args[i] = sysRole.getId();
				}
				sqlStr = sqlStr.substring(0, sqlStr.lastIndexOf(","));
				sqlStr +=" )";
				log.debug(sqlStr);
				return jdbcTemplate.query(sqlStr.toString(), args, new SysButtonRelMapper());
			}
		}catch(Exception e){
			e.printStackTrace();
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		
		return null;
	}
}

